package net.fltx.taskmgr.util;

import net.fltx.taskmgr.entity.ProcEntry;
import net.fltx.taskmgr.win32api.Kernel32;
import net.fltx.taskmgr.win32api.ModuleEntry32;
import net.fltx.taskmgr.win32api.ProcessEntry32;

import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class ProcessUtil {
    /**
     * 获取系统所有进程
     */
    public static void getProcessList(){
        Kernel32 kernel32 = Kernel32.INSTANCE;
        int hSnapShot = kernel32.CreateToolhelp32Snapshot(15, 0);
        ProcessEntry32 processEntry32 = new ProcessEntry32.ByReference();
        List<ProcEntry> pes=new ArrayList<>();

        ProcEntry pe;
        for(int lpid = kernel32.Process32First(hSnapShot, processEntry32); lpid != 0; pes.add(pe)) {
            lpid = kernel32.Process32Next(hSnapShot, processEntry32.free());
            pe = new ProcEntry();
            pe.setPid(processEntry32.th32ProcessID);

            try {
                pe.setProcName((new String(processEntry32.szExeFile, "gbk")).trim());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        System.out.println(pes);


    }

    /**
     * 获取进程所有模块
     * @param pid 进程id
     */
    public static void getProcessModules(int pid){
        Kernel32 kernel32 = Kernel32.INSTANCE;
        int hSnapShot = kernel32.CreateToolhelp32Snapshot(15, pid);
        ModuleEntry32 tme=new ModuleEntry32();
        if(kernel32.Module32First(hSnapShot,tme)!=0){
            System.out.println(tme);
            while (kernel32.Module32Next(hSnapShot,tme.free())!=0){
                System.out.println(tme);
            }
        }
    }


    /**
     * 获取进程图标
     * @param pid 进程id
     * @return
     */
    public static BufferedImage getProcessIcon(int pid){
        return null;
    }



}
